package Service;

import Domain.*;
import utils.JDBCutils;
import utils.employees;
import utils.eqipments;

import java.sql.SQLInput;
import java.util.ArrayList;
import java.util.List;
import java.util.SplittableRandom;

/**
 * ClassName:NameListService
 * Package:Service
 * Description
 *
 * @Author:@wenxueshi
 * @Create:2022/11/30 - 18:25
 * @Version:v1.0
 */
public class NameListService {
    private ArrayList<Employee> list = new ArrayList();
    List<employees> empList;

    {
        String sql = "select * from employees";
        //数据库取员工数据
        empList = JDBCutils.getSelect(employees.class, sql);
    }

    //构造器做赋值操作
    public NameListService() {
        for (int i = 0; i < empList.size(); i++) {
            employees empl = empList.get(i);
            int index = empl.getIndex();
            int id = empl.getId();
            String name = empl.getName();
            int age = empl.getAge();
            int salary = empl.getSalary();
            Equipment eqipment;
            int bonus;
            int stock;
            switch (index) {
                case 10:
                    list.add(new Employee(id, name, age, salary));
                    break;
                case 11:
                    eqipment = getEquiment(i-1);
                    list.add(new Programmer(id, name, age, salary, eqipment));
                    break;
                case 12:
                    eqipment = getEquiment(i-1);
                    bonus = empl.getBonus();
                    list.add(new Designer(id, name, age, salary, eqipment, bonus));
                    break;
                case 13:
                    eqipment = getEquiment(i-1);
                    bonus = empl.getBonus();
                    stock = empl.getStock();
                    list.add(new Architect(id, name, age, salary, eqipment, bonus, stock));
                    break;
            }
        }
    }

    /**
     * 获取员工设备
     *
     * @return
     */
    public Equipment getEquiment(int flag) {
        String sql = "select * from eqipments";
        List<eqipments> select = JDBCutils.getSelect(eqipments.class, sql);
        eqipments eqip = select.get(flag);
        int index = eqip.getIndex();
        String type = eqip.getType();
        String show = eqip.getShow();
        switch (index)
        {
            case 21:
                return new PC(type,show);
            case 22:
                return new NoteBook(type,show);
            case 23:
                return new Printer(type,show);
        }
        return null;
    }

    /**
     * 获取所有员工
     *
     * @return
     */
    public ArrayList<Employee> getAllempl() {
        return list;
    }

    /**
     * 获取指定位置上的员工
     * @param id
     * @return
     */
    public Employee getIndexEmpl(int id) throws TeamException {
        for(int i=0;i<list.size();i++)
        {
            if(list.get(i).getId() == id)
            {
                return list.get(i);
            }
        }
        throw  new TeamException("找不到指定的员工");
    }
}
